package com.xie.config;

import com.xie.context.BaseContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FeignConfig {
    /**
     * 配置 Feign 的日志级别为 FULL（最详细）
     */
    @Bean
    public Logger.Level fullFeignLoggerLevel() {
        return Logger.Level.FULL;
    }
    /**
     * 添加请求拦截器，透传用户信息到请求头
     */
    @Bean
    public RequestInterceptor userInfoRequestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                Long userId = BaseContext.getCurrentId(); // 从线程上下文获取用户ID
                if (userId != null) {
                    template.header("user-id", userId.toString()); // 添加请求头
                }
            }
        };
    }
}